package com.yu.unregrister;

import com.yu.transport.server.NettyServer;
import com.yu.util.CuratorUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * @PackageName: com.yu.unregrister
 * @Description:
 * @Version: v1.0.0
 * @Author: yuweilin
 * @Date: 10:45
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/1/27      yuweilin         v1.0.0               新建
 */
@Slf4j
public class CustomShutdownHook {
    private static final CustomShutdownHook CUSTOM_SHUTDOWN_HOOK = new CustomShutdownHook();

    public static CustomShutdownHook getCustomShutdownHook(){ return CUSTOM_SHUTDOWN_HOOK;}

    public void clearAll(){
        log.info("clear all service");
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            try {
                InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), NettyServer.PORT);
                CuratorUtil.clearRegistrar(CuratorUtil.getZkClient(),inetSocketAddress);
            }catch (UnknownHostException e){
                log.error("获取本机的地址失败");
        }
        }));
    }
}
